package com.xuelang.pipeline.artifact.app.nexus3v1.fallback;

import com.xuelang.pipeline.artifact.app.nexus3v1.ComponentManagementService;
import com.xuelang.pipeline.artifact.app.nexus3v1.exception.ArtifactException;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.GetComponentResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.ListComponentResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import org.springframework.cloud.openfeign.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author xiwen.zxg
 * @since 2024/7/10
 */
@Slf4j
@Service
public class ComponentManagementServiceFallbackFactory implements FallbackFactory<ComponentManagementService> {
    @Override
    public ComponentManagementService create(Throwable throwable) {
        ApiResponseXO<Void> apiResponseXO = new ApiResponseXO<>();
        if (throwable instanceof ArtifactException) {
            ArtifactException nexus3Exception = (ArtifactException) throwable;
            apiResponseXO = nexus3Exception.getApiResponse();
        }
        final ApiResponseXO resultApiResponseXO = ApiResponseXO.builder()
                .status(apiResponseXO.getStatus())
                .id(apiResponseXO.getId())
                .message(apiResponseXO.getMessage())
                .data(apiResponseXO.getData())
                .build();
        log.error("{} happened to exception with {}", this.getClass().getName(), throwable.getMessage());
        return new ComponentManagementService() {


            @Override
            public ApiResponseXO<ListComponentResponse> componentList(String continuationToken, String repository) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> uploadComponent(String repository, MultipartFile rAsset, String rAssetPathId, MultipartFile pypiAsset, MultipartFile helmAsset, String yumDirectory, MultipartFile yumAsset, String yumAssetFilename, MultipartFile dockerAsset, MultipartFile rubygemsAsset, MultipartFile nugetAsset, MultipartFile npmAsset, String rawDirectory, MultipartFile rawAsset1, String rawAsset1Filename, MultipartFile rawAsset2, String rawAsset2Filename, MultipartFile rawAsset3, String rawAsset3Filename, MultipartFile aptAsset, String maven2GroupId, String maven2ArtifactId, String maven2Version, Boolean maven2GeneratePom, String maven2Packaging, MultipartFile maven2Asset1, String maven2Asset1Classifier, String maven2Asset1Extension, MultipartFile maven2Asset2, String maven2Asset2Classifier, String maven2Asset2Extension, MultipartFile maven2Asset3, String maven2Asset3Classifier, String maven2Asset3Extension) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<GetComponentResponse> getComponent(String id) {
                return resultApiResponseXO;
            }

            @Override
            public ApiResponseXO<Void> deleteComponent(String id) {
                return resultApiResponseXO;
            }
        };
    }
}
